//
//  NiceImgViewController.m
//  RoadLine
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 RedFireNoodles. All rights reserved.
//

#import "NiceImgViewController.h"
#import "NiceImgCollectionViewCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "NicePic.h"
#import "MJRefresh.h"
#import "TripDetailViewController.h"

// 定义宏
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface NiceImgViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

// collectionView
@property(nonatomic, retain)UICollectionView *collect;
// 用来装解析后的对象
@property(nonatomic, retain)NSMutableArray *collectionArr;
// 数据刷新的网址页数
@property(nonatomic, assign)NSInteger page;

@end

@implementation NiceImgViewController

// 初始化
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    self.collectionArr = [NSMutableArray array];
        self.title = @"每日精选图片";
    }
    return self;
    
}

// 数据刷新
- (void)load{
    [self.collect addLegendHeaderWithRefreshingBlock:^{
    [self.collect.header endRefreshing];
    }];
    [self.collect.legendHeader beginRefreshing];
    [self.collect addLegendFooterWithRefreshingBlock:^{
    [self data];
    [self.collect reloadData];
    [self.collect.footer endRefreshing];
    }];
}


// 数据处理
-(void)data{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSInteger number = self.page * 12;
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/featured/photos/?start=%ld&count=12",number];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"items"];
        for (NSMutableDictionary *dic in arr) {
            NicePic *nice = [[NicePic alloc] init];
            [nice setValuesForKeysWithDictionary:dic];
            [self.collectionArr addObject:nice];
            [nice release];
        }
        [self.collect reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      
    }];
    self.page++;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // collectionView瀑布流设置
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake(WIDTH/2 - 35, WIDTH/2 - 35);
    flowLayout.minimumInteritemSpacing = 1;
    flowLayout.minimumLineSpacing = WIDTH/10 - 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(WIDTH/30, WIDTH/15, 30, WIDTH/15);

    
    // collectionView设置
    self.collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) collectionViewLayout:flowLayout];
    self.collect.delegate = self;
    self.collect.dataSource = self;
    [self.view addSubview:self.collect];
    [self.collect registerClass:[NiceImgCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.collect.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];

    self.page = 0;
    [self load];
    [self data];
    [flowLayout release];
    [_collect release];
    //修改UInavigationBar的字体颜色(系统API没有这个设置 只能自己建一个Label覆盖 然后修改label的属性)
    UILabel *titleLabelPrice = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabelPrice.font = [UIFont boldSystemFontOfSize:18];
    titleLabelPrice.textColor = [UIColor whiteColor];
    titleLabelPrice.backgroundColor = [UIColor clearColor];
    titleLabelPrice.text = @"   每日图片精选";
    self.navigationItem.titleView = titleLabelPrice;
    [titleLabelPrice release];
    
    if ([Singleton shareSingleton].isNight) {
        [[Singleton shareSingleton] loadNightModeInfoFormDisk];
    }
}

// 设置白天模式
- (void)setLightDayModel {
    self.collect.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:246/255.0 green:85/255.0 blue:91/255.0 alpha:1];

}

// 设置夜间模式
- (void)setNightDayModel {
    self.collect.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:4/255.0 green:165/255.0 blue:171/255.0 alpha:1];
}

 // collectionView必须实现的方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.collectionArr.count;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NiceImgCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NicePic *nice = self.collectionArr[indexPath.row];
    cell.myLabel.text = nice.trip_name;
    NSURL *url = [NSURL URLWithString:nice.photo_s];
    [cell.myImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"BEIJING.png"]];
    return cell;
}

// collectionView 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TripDetailViewController *niceVC = [[TripDetailViewController alloc] init];
    NicePic *niceJupe = self.collectionArr[indexPath.row];
    niceVC.tripDetailViewText = niceJupe.text;
    niceVC.tripDetailViewUrl = niceJupe.photo_s;
    [self.navigationController pushViewController:niceVC animated:YES];
    [niceVC release];
}

-(void)dealloc{
    
    [_collect release];
    [_collectionArr release];
    [super dealloc];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
